home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_46 / recdma.c < prev    next >
C/C++ Source or Header  |  1995-01-01  |  2KB  |  108 lines

  1. /* Record from SB DAC using DMA mode (up to 64K only) */
  2.  
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <dos.h>
  6. #include <io.h>
  7. #include <alloc.h>
  8. #include <stdlib.h>
  9.  
  10. #include "sb.h"
  11.  
  12. extern int optind;    /* index of which argument is next      */
  13. extern char *optarg;  /* pointer to argument of current option */
  14. extern int opterr;    /* allow error message  */
  15. int getopt(int argc, char *argv[], char *optionS);
  16.  
  17. void main(int argc, char *argv[])
  18. {
  19.     FILE *f;
  20.     signed char far *raw, far *aligned;
  21.     unsigned sl, sr=11000;
  22.     unsigned long physical, aligned_physical;
  23.     int stereo = 0, error = 0;
  24.     char ch;
  25.  
  26.     while((ch = getopt(argc,argv,"sr:")) != EOF)
  27.     {
  28.     switch(ch)
  29.     {
  30.         case 's':
  31.         stereo = 1;
  32.         break;
  33.         case 'r':
  34.         sr = atoi(optarg);
  35.         break;
  36.         case '?':
  37.         error++;
  38.         break;
  39.     }
  40.     }
  41.     if(error || optind == argc)
  42.     {
  43.     puts("Usage: recdma [-r rate] [-s] sample");
  44.     exit(1);
  45.     }
  46.  
  47.     if(Sb_Get_Params())
  48.     {
  49.         puts("BLASTER environment variable not set.");
  50.         exit(1);
  51.     }
  52.  
  53.     if(Sb_Init())
  54.     {
  55.     printf("Could not find Soundblaster!\n");
  56.     exit(1);
  57.     }
  58.     printf("Found Soundblaster at address %xh, IRQ %d, DMA %d.\n",
  59.     SbIOaddr,SbIRQ,SbDMAchan);
  60.  
  61.     f = fopen(argv[optind],"wb");
  62.     if(f == NULL)
  63.     {
  64.     printf("Could not open sample file %s\n",argv[optind]);
  65.     exit(1);
  66.     }
  67.  
  68.     sl = 64000U;
  69.  
  70.     raw = (signed char far *)farmalloc((unsigned long)sl + 65535L);
  71.     physical = ((unsigned long)FP_OFF(raw)) + (((unsigned long)FP_SEG(raw)) << 4);
  72.     aligned_physical = physical+0x0FFFFL;
  73.     aligned_physical &= 0xF0000L;
  74.     aligned=MK_FP((unsigned )((aligned_physical >> 4) & 0xFFFF),0);
  75.  
  76.     Sb_Init_Voice_DMA(NULL);   /* Use default interrupt handler */
  77.  
  78.     sr = Sb_Sample_Rate(sr,RECORD);
  79.     printf("Sample rate = %u Hz\n",sr);
  80.     printf("Input is %s.\n",(stereo) ? "stereo" : "mono");
  81.  
  82.     Sb_Voice(0);
  83.     printf("Recording sample\n");
  84.  
  85.     Sb_Voice_DMA(aligned,sl,stereo,RECORD);
  86.  
  87.     while(!kbhit() && !Sb_DMA_Complete())
  88.     ;
  89.     if(!Sb_DMA_Complete())
  90.     {
  91.     Sb_Halt_DMA();
  92.         sl = dma_addr(SbDMAchan);  /* Find the shortened length */
  93.         Sb_Init(); /* Necessary after early termination of a high-speed xfer */
  94.     getch();
  95.     }
  96.  
  97.     fwrite(aligned,1,sl,f);
  98.     fclose(f);
  99.  
  100.     printf("Done.\n");
  101.  
  102.     Sb_DeInit_Voice_DMA();
  103.  
  104.     farfree(raw);
  105.  
  106.     exit(0);
  107. }
  108.